home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / cutNpaste / Motif+Xt / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.8 KB  |  138 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "data.h"
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23.  
  24. static char* currentMolecule = NULL;
  25.  
  26. char *getSelection() {
  27.     if (currentMolecule)
  28.         return strdup(currentMolecule);
  29.     else
  30.         return NULL;
  31. }
  32.  
  33. void parseElement(char *s) {
  34.     char name[ELEMNAMESIZE];
  35.     float radius, red, green, blue;
  36.  
  37.     sscanf(s, "%[A-Za-z] = %f ( %f, %f, %f )", name, &radius, &red, &green, &blue);
  38.     elementRoot = createElement(elementRoot, name, radius, red, green, blue);
  39. }
  40.     
  41. void parseAtom(char *s, struct molecule *m) {
  42.     char name[ELEMNAMESIZE];
  43.     float x, y, z;
  44.  
  45.     sscanf(s, "%[A-Za-z] : %f %f %f", name, &x, &y, &z);
  46.     addAtom(m, name, x, y, z);
  47. }
  48.  
  49.  
  50. void parse(char *s, struct molecule *m) {
  51.     char name[ELEMNAMESIZE];
  52.     char type[ELEMNAMESIZE];
  53.  
  54.     if ((s[0] == '\0') || (s[0] == ';'))
  55.         return;
  56.  
  57.     sscanf(s, "%[A-Za-z] %[:=]", name, type);
  58.  
  59.     if (!type)
  60.         return;
  61.  
  62.     switch(type[0]) {
  63.         case ':':
  64.             parseAtom(s, m);
  65.             break;
  66.         case '=':
  67.             parseElement(s);
  68.             break;
  69.         default:
  70.             break;
  71.     }
  72. }
  73.             
  74. void buildMolecule(char *text) {
  75.     char line[80];
  76.     char *lp, *tp;
  77.     struct molecule *m;
  78.     if (!text)
  79.         return;
  80.  
  81.     if(currentMolecule)
  82.         free(currentMolecule);
  83.     currentMolecule = text;
  84.         
  85.     moleculeRoot = freeMolecules(moleculeRoot);
  86.     elementRoot = freeElements(elementRoot);
  87.     m = moleculeRoot = createMolecule(moleculeRoot, "Test");
  88.  
  89.     tp = text;
  90.     while(*tp != '\0') {
  91.         lp = line;
  92.         while((*tp != '\n') && (*tp != '\0'))
  93.             *lp++ = *tp++;
  94.         *lp = '\0';
  95.         tp++;
  96.         parse(line, m);
  97.     }
  98. }
  99.  
  100. char *readFile(FILE *fp, char *theFilename)
  101. {
  102.     struct stat theStats;
  103.     char *fileBuffer;
  104.  
  105.     if (stat(theFilename, &theStats) < 0){
  106.         perror("stat");
  107.         exit(1);
  108.     }
  109.     fileBuffer = calloc(theStats.st_size+1, 1);
  110.     if (!fileBuffer)
  111.         return;
  112.  
  113.     fread(fileBuffer, 1, theStats.st_size, fp);
  114.     if (ferror(fp)) {
  115.         free(fileBuffer);
  116.         fileBuffer = NULL;
  117.     }
  118.     fclose(fp);
  119.     return fileBuffer;
  120. }
  121.  
  122. void openFile(char *theFilename)
  123. {
  124.     FILE *fp;
  125.     char *fileBuffer;
  126.     if (!theFilename)
  127.         return;
  128.  
  129.     fp = fopen(theFilename, "r");
  130.     if (!fp)
  131.         return;
  132.     if (!(fileBuffer = readFile(fp, theFilename)))
  133.         return;
  134.     buildMolecule(fileBuffer);
  135.  
  136. }    
  137.  
  138.